//
//  ViewController.m
//  UIDatePicker
//
//  Created by 移云 on 2019/11/19.
//  Copyright © 2019 MaoYeYeDeJiangCuCha. All rights reserved.
//

#import "ViewController.h"
#import <Masonry/Masonry.h>
@interface ViewController ()

//懒加载控件用strong
//输入框
@property(nonatomic,strong)UITextField *textField;

//时间选择器
@property(nonatomic,strong)UIDatePicker *datePicker;

//工具条
@property(nonatomic,strong)UIToolbar *toolBar;

@end

@implementation ViewController

#pragma mark - UIDatePicker懒加载
- (UIDatePicker *)datePicker{
    if (_datePicker == nil) {
        //不需要设置frame,自动占据
        _datePicker = [[UIDatePicker alloc]init];
        // 日期模式
        _datePicker.datePickerMode = UIDatePickerModeDate;
        //本地化 local 在语言中查看
        _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"];
        
    }
    return _datePicker;
}

#pragma mark - UIToolBar
- (UIToolbar *)toolBar{
    if (_toolBar == nil) {
        //只需要设置它的高度，系统会自动撑开
        _toolBar = [[UIToolbar alloc]init];
        _toolBar.frame = CGRectMake(0, 0, 0, 44);
        
        //创建按钮放到工具条里面
        //取消
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelItemClick)];
        
        //弹簧
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        //确认
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(doneItemClick)];
        
        _toolBar.items = @[cancelItem,flexSpace,doneItem];
        
    }
    
    return _toolBar;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //获取info.plist文件
    NSDictionary *dict = [NSBundle mainBundle].infoDictionary;
    NSLog(@"%@",dict);
    
    self.textField = [[UITextField alloc]init];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    //设置文本框的输入界面为datePicker
    self.textField.inputView = self.datePicker;
    //设置工具条
    self.textField.inputAccessoryView = self.toolBar;
    [self.view addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(80);
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);

    }];
    
    
    
}

//点击取消按钮的调用
- (void)cancelItemClick {
    
    [self.view endEditing:YES];
    
}

//点击确认按钮的调用
-(void)doneItemClick {
    
    //1.获取日期
    NSDate *date = self.datePicker.date;
    
    //2.将选择的日期赋值给文本框
    //将日期转为字符串
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy年MM月dd日";
   
    //赋值给文本框
    self.textField.text =  [formatter stringFromDate:date];
    
    //3.关闭键盘
    [self.view endEditing:YES];
    
}


@end
